package d20191212;

import cn.itcast.xml.XmlUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Demo1 {
    static List<Student> slist = XmlUtils.readAll("student.xml", Student.class);
    static List<Classes> clist = XmlUtils.readAll("classes.xml", Classes.class);

    public static void main(String[] args) throws Exception {
        for (Classes c : clist) {
            for (Student s : slist) {
                if (c.getClassID().equals(s.getClassID())) {
                    c.getStudents().add(s);
                }
            }
        }
        System.out.println(clist);
        getAvg();
        MathGpa();
        mathFile();
        underAge();
        sort();
    }

    public static void getAvg() {
        List<Integer> integers = new ArrayList<>();
        for (int i = 0; i < clist.size(); i++) {
            int avg = 0;
            int num = 0;
            for (int j = 0; j < clist.get(i).getStudents().size(); j++) {
                avg += clist.get(i).getStudents().get(j).getAge();
                num++;
            }
            integers.add(avg / num);
        }
        int maxAge = 0, number = 0;
        for (int i = 0; i < integers.size(); i++) {
            if (integers.get(i) > maxAge) {
                maxAge = integers.get(i);
                number = i;
            }
        }
        for (int j = 0; j < clist.size(); j++) {
            if (number == j) {
                System.out.println("班级编号：" + clist.get(j).getClassID() +
                        "\t班级：" + clist.get(j).getClassname() +
                        "\t讲师：" + clist.get(j).getTeacher() +
                        "\t助理讲师：" + clist.get(j).getAssistant() +
                        "\t辅导员：" + clist.get(j).getInstructor() + maxAge);
            }
        }
    }

    public static void MathGpa() {
        List<Double> doubles = new ArrayList<>();
        for (int i = 0; i < clist.size(); i++) {
            double mathapg = 0.0, num = 0.0;
            for (int j = 0; j < clist.get(i).getStudents().size(); j++) {
                mathapg += clist.get(i).getStudents().get(j).getMath();
                num++;
            }
            doubles.add(mathapg / num);
        }
        int number = 0;
        double mathmin = 100.0;
        for (int i = 0; i < doubles.size(); i++) {
            if (doubles.get(i) < mathmin) {
                mathmin = doubles.get(i);
                number = i;
            }
        }
        for (int i = 0; i < clist.size(); i++) {
            if (i == number) {
                System.out.println("班级编号：" + clist.get(i).getClassID() +
                        "\t班级：" + clist.get(i).getClassname() + "\t成绩：" +
                        mathmin);
            }
        }
    }

    public static void mathFile() {
        List<Integer> integers = new ArrayList<>();
        for (int i = 0; i < clist.size(); i++) {
            int filepeople = 0;
            for (int j = 0; j < clist.size(); j++) {
                if (clist.get(i).getStudents().get(j).getMath() < 60) {
                    filepeople++;
                }
            }
            integers.add(filepeople);
        }
        int maxfile = 0, num = 0;
        for (int i = 0; i < integers.size(); i++) {
            if (integers.get(i) > maxfile) {
                maxfile = integers.get(i);
                num = i;
            }
        }
        for (int i = 0; i < clist.size(); i++) {
            if (i == num) {
                System.out.println("班级编号：" + clist.get(i).getClassID() +
                        "\t班级：" + clist.get(i).getClassname() + "\t不及格人数：" +
                        maxfile);
            }
        }
    }

    public static void underAge() {
        List<String>integers=new ArrayList<>();
        for (int i = 0; i < clist.size(); i++) {
            int underage = 0;
            for (Student student : clist.get(i).getStudents()) {
                if (student.getAge()< 18) {
                    underage++;
                }
            }
            integers.add(clist.get(i).getClassname()+"\t人数："+(underage));
        }
        for (int i = 0; i < integers.size(); i++) {
            System.out.println("班级："+integers.get(i));
        }
    }

    public static void sort()throws Exception{
        for (Classes classes : clist) {
            classes.getStudents().sort(new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    if (o1.getAge()-o2.getAge()<0){
                        return -1;
                    }
                    if (o1.getAge()-o2.getAge()==0 && o1.getMath()-o2.getMath()<0){
                        return -1;
                    }
                    return 0;

                }
            });
            System.out.println(classes.toString());
            for (int i = 0; i < clist.size(); i++) {
                String str = "";
                for (int j = 0; j < clist.get(i).getStudents().size(); j++) {
                    if (clist.get(i).getClassID().equals(clist.get(i).getStudents().get(j).getClassID())) {
                        str += clist.get(i).getStudents().get(j).getNumber()+" 姓名："+clist.get(i).getStudents().get(j).getName()
                                +" 年龄："+clist.get(i).getStudents().get(j).getAge()+" 性别："+clist.get(i).getStudents().get(j).getGender()
                                +" 数学成绩："+clist.get(i).getStudents().get(j).getMath()+" 班级编号："+clist.get(i).getStudents().get(j).getClassID()+"\r\n";
                    }
                }
                FileUtils.write(new File(clist.get(i).getClassID()+".txt"),str,false);
            }
        }
    }
}